/**
 * 元数据管理相关类型定义
 */

import type {BaseEntity, PageResponse} from '../common/base'

/**
 * 数据库类型枚举
 */
export enum DatabaseType {
  MYSQL = 'MYSQL',
  ORACLE = 'ORACLE',
  POSTGRESQL = 'POSTGRESQL',
  OCEANBASE = 'OCEANBASE'
}

/**
 * 模式实体
 */
export interface Schema extends BaseEntity {
  productId: number
  schemaName: string
  schemaType: DatabaseType
  schemaDesc?: string
  isEnabled: boolean
  totalTables: number
}

/**
 * 表实体
 */
export interface Table extends BaseEntity {
  productId: number
  schemaId: number
  tableName: string
  tableComment?: string
  tableType: string
  engine?: string
  charset?: string
  collation?: string
  rowCount?: number
  dataLength?: number
  indexLength?: number
  isEnabled: boolean
}

/**
 * 列实体
 */
export interface Column extends BaseEntity {
  productId: number
  schemaId: number
  tableId: number
  columnName: string
  columnType: string
  dataType: string
  columnLength?: number
  columnScale?: number
  isNullable: boolean
  isPrimaryKey: boolean
  isAutoIncrement: boolean
  columnDefault?: string
  columnComment?: string
  ordinalPosition: number
  isEnabled: boolean
}

/**
 * 约束实体
 */
export interface Constraint extends BaseEntity {
  productId: number
  schemaId: number
  tableId: number
  constraintName: string
  constraintType: 'PRIMARY_KEY' | 'FOREIGN_KEY' | 'UNIQUE' | 'CHECK'
  columnName?: string
  referencedTable?: string
  referencedColumn?: string
  constraintDefinition?: string
  isEnabled: boolean
}

/**
 * 索引实体
 */
export interface Index extends BaseEntity {
  productId: number
  schemaId: number
  tableId: number
  indexName: string
  indexType: string
  isUnique: boolean
  isPrimary: boolean
  columnName?: string
  indexDefinition?: string
  isEnabled: boolean
}

/**
 * 数据字典实体
 */
export interface Dictionary extends BaseEntity {
  productId: number
  dictType: string
  dictKey: string
  dictValue: string
  dictLabel?: string
  dictComment?: string
  isEnabled: boolean
  sortOrder?: number
}

/**
 * 模式查询参数
 */
export interface SchemaSearchParams {
  productId?: number
  schemaName?: string
  schemaType?: DatabaseType
  isEnabled?: boolean
  page?: number
  size?: number
}

/**
 * 表查询参数
 */
export interface TableSearchParams {
  productId?: number
  schemaId?: number
  tableName?: string
  isEnabled?: boolean
  page?: number
  size?: number
}

/**
 * 列查询参数
 */
export interface ColumnSearchParams {
  productId?: number
  schemaId?: number
  tableId?: number
  columnName?: string
  isNullable?: boolean
  isPrimaryKey?: boolean
  page?: number
  size?: number
}

/**
 * DDL导出请求
 */
export interface DDLExportRequest {
  productId: number
  schemaIds?: number[]
  tableIds?: number[]
  targetDatabaseType: DatabaseType
  includeDropStatement?: boolean
  includeCreateStatement?: boolean
  includeIndex?: boolean
  includeConstraint?: boolean
  includeComment?: boolean
}

/**
 * DDL导出响应
 */
export interface DDLExportResponse {
  taskId: string
  fileName: string
  content: string
  fileSize: number
  exportTime: string
}

/**
 * 统计信息
 */
export interface MetadataStats {
  totalProducts: number
  totalSchemas: number
  totalTables: number
  totalColumns: number
  totalIndexes: number
  totalConstraints: number
  databaseTypes: Array<{
    type: DatabaseType
    count: number
  }>
}

// 分页响应类型
export type SchemaPageResponse = PageResponse<Schema>
export type TablePageResponse = PageResponse<Table>
export type ColumnPageResponse = PageResponse<Column>
export type ConstraintPageResponse = PageResponse<Constraint>
export type IndexPageResponse = PageResponse<Index>
export type DictionaryPageResponse = PageResponse<Dictionary>